import * as THREE from "three";
import * as TWEEN from '@tweenjs/tween.js'
import { MeshLine, MeshLineMaterial } from 'three.meshline';


export class AnimationManager {
  constructor(scene) {
    this.scene = scene;
  }
  /**
   *添加飞线动画
   *
   * @param {String} name 飞线名称
   * @param {Array} points 飞线路径坐标组
   * @param {Float} ratio 飞线在线路中比例
   * @param {Float} width 飞线宽度
   * @param {Float} speed 飞线速度
   * @param {THREE.Color} color 飞线颜色
   * @param {THREE.Texture} tex 飞线纹理
   * @return {TWEEN.Tween} 飞线动画
   * @memberof AnimationManager
   */
  createMovedLine(name, points, ratio, width, speed, color, tex) {
    let len = Math.floor(points.length * ratio);
    const pointOnLine = points.slice(0, len);
    const movedLineGeo = new MeshLine();
    const linePoints = [];
    for (let i = 0; i < pointOnLine.len; i++) {
      linePoints.push(pointOnLine[i].x, pointOnLine[i].y, pointOnLine[i].z);
    }
    movedLineGeo.setPoints(linePoints);


    const lineMtl = new MeshLineMaterial({
      useMap: 1,
      color: color,
      opacity: 1,
      //resolution: new THREE.Vector2(window.innerWidth, window.innerHeight),
      //sizeAttenuation: 1,
      lineWidth: width,
      side: THREE.DoubleSide,
      transparent: true,
      wireframe: false,
      map: tex
    })
    const movedLine = new THREE.Mesh(movedLineGeo, lineMtl);
    movedLine.name = name;
    this.scene.add(movedLine);

    const moveTime = 1 / speed * 1000;
    const tween = new TWEEN.Tween({ index: 0 }).to({ index: 100 }, moveTime)
      .onUpdate((t) => {
        let movedLineGeo = movedLine.geometry;
        let currentIndex = t.index
        let id = Math.ceil(currentIndex);
        let pointsList = points.slice(id, id + len);
        let pointsArr = []
        for (let i = 0; i < pointsList.length; i++) {
          pointsArr.push(pointsList[i].x, pointsList[i].y, pointsList[i].z)
        }
        movedLineGeo && movedLineGeo.setPoints(pointsArr);
        movedLineGeo.attributes.position.needsUpdate = true;
      }).repeat(Infinity);

    return tween;
    //tween.start();

  }

  /**
   * 添加线路生成动画
   *
   * @param {String} name 线路名称
   * @param {Array} points  线路起点终点坐标组
   * @param {Float} width 线路宽度
   * @param {THREE.Color} color 线路颜色
   * @param {Float} speed 线路速度
   * @return {TWEEN.Tween} 线路生成动画
   * @memberof AnimationManager
   */
  generateLine(name, points, width, color, speed) {
    const curve = new THREE.CatmullRomCurve3(points);
    const geometry = new THREE.BufferGeometry().setFromPoints(curve.getPoints(50));
    const line = new MeshLine();
    line.setGeometry(geometry);
    const lineMtl = new MeshLineMaterial({
      useMap: 0,
      color: color,
      opacity: 1,
      lineWidth: width,
      side: THREE.DoubleSide,
      transparent: true,
      wireframe: false,
    });
    const generateLine = new THREE.Mesh(line.geometry, lineMtl);
    generateLine.name = name;

    this.scene.add(generateLine);
    const generateTime = 1 / speed * 1000;
    const tween = new TWEEN.Tween({ count: 0 })
      .to({ count: generateLine.geometry.attributes.position.count * 3 }, generateTime)
      .easing(TWEEN.Easing.Linear.None)
      .onUpdate(function (t) {
        generateLine.geometry.setDrawRange(0, t.count)
      });

    return tween;
  }




}